package com.horsen.service.edu.controller.admin;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.common.util.ResultCodeEnum;
import com.horsen.service.base.exception.HseduException;
import com.horsen.service.base.util.ExceptionUtils;
import com.horsen.service.edu.controller.BaseController;
import com.horsen.service.edu.model.VO.SubjectChartVO;
import com.horsen.service.edu.model.VO.SubjectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 科目 控制器
 *
 * @author Horsen
 */
@Api(tags = "科目管理")
@ApiSupport(order = 3)
@Slf4j
@RestController("AdminSubject")
@RequestMapping("/admin/edu/subjects")
public class SubjectController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("通过Excel批量导入科目")
    @PostMapping("/")
    public R batchImport(@ApiParam(value = "Excel文件", required = true) MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            subjectService.batchImport(inputStream);
            return R.ok("批量导入科目成功");
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new HseduException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("获取科目树形列表")
    @GetMapping("/")
    public R getTreeList() {
        List<SubjectVO> treeList = subjectService.getTreeList();
        return R.ok().setData("items", treeList);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("获取二级科目数")
    @GetMapping("/count")
    public R count() {
        Integer count = subjectService.countSecondarySubject();
        return R.ok("获取二级科目数成功").setData("count", count);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("获取科目饼状图数据")
    @GetMapping("/chart")
    public R chart() {
        List<SubjectChartVO> subjectChart = subjectService.getChartValue();
        return R.ok("获取科目饼状图数据成功").setData("subjectChart", subjectChart);
    }
}

